View Javadoc
1 package jrre.gui; 2 3 import java.io.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 import java.beans.*; 8 9 /*** 10 * General purpose text editor. Register parent class with property 11 * change listener to update resource when the editor is closed. 12 * Usage: 13 * <pre> 14 * TextEditor editor = new TextEditor(); 15 * editor.setResource(resource); 16 * editor.setText(text); 17 * editor.setVisible(true); 18 * </pre> 19 * Use the setResource(String) method to set the resource that gets 20 * updated when a property change event is fired. 21 * 22 * @author Christopher Chess Ellsworth (chris@chrisellsworth.com) 23 */ 24 public class TextEditor extends JFrame implements ActionListener, 25 Serializable { 26 27 private String resource; 28 private JButton okButton; 29 private JTextArea textArea = new JTextArea(); 30 private PropertyChangeSupport propertyChange = new PropertyChangeSupport(this); 31 32 /*** 33 * Constructor for the text editor. 34 */ 35 public TextEditor(){ 36 37 super("Editor"); 38 39 String iconPrefix = 40 new String("lib/icons/toolbarButtonGraphics/"); 41 42 okButton = new JButton("Ok", 43 new ImageIcon(iconPrefix+"general/Save24.gif")); 44 45 okButton.setActionCommand("Ok"); 46 okButton.addActionListener(this); 47 48 Container content = getContentPane(); 49 content.setLayout(new BorderLayout()); 50 51 JScrollPane scrollPane = new JScrollPane(textArea); 52 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 53 scrollPane.setPreferredSize(new Dimension(250, 250)); 54 55 content.add(scrollPane); 56 content.add(okButton, BorderLayout.SOUTH); 57 58 setSize(600,600); 59 setLocation(200,50); 60 } 61 62 /*** 63 * Handles action event when the Ok button is pressed. 64 */ 65 public void actionPerformed(ActionEvent event){ 66 67 String action = event.getActionCommand(); 68 69 if(action.equals("Ok")) 70 closeEditor(); 71 } 72 73 /*** 74 * Closes the editor and notifies any registered action listeners. 75 */ 76 public void closeEditor(){ 77 setVisible(false); 78 propertyChange.firePropertyChange(resource, "old",textArea.getText()); 79 } 80 81 /*** 82 * Sets the resource to be edited. This is the String that is returned 83 * from the <code>event.getPropertyName()</code> method when a property change is 84 * fired. 85 */ 86 public void setResource(String newResource){ resource = newResource; } 87 88 /*** 89 * Gets the resource that is currently being edited. 90 */ 91 public String getResource(){ return resource; } 92 93 /*** 94 * Sets the text to display in the editor. 95 */ 96 public void setText(String newText){ 97 textArea.setText(newText); 98 } 99 100 /*** 101 * Gets the text currently in the editor. 102 */ 103 public String getText(){ return textArea.getText(); } 104 105 /*** 106 * Adds a listener to the collection of classes to notify when a 107 * property changes. 108 */ 109 public void addPropertyChangeListener(PropertyChangeListener listener) { 110 propertyChange.addPropertyChangeListener(listener); 111 } 112 113 /*** 114 * Removes a listener to the collection of classes to notify when a 115 * property changes. 116 */ 117 public void removePropertyChangeListener(PropertyChangeListener listener) { 118 propertyChange.removePropertyChangeListener(listener); 119 } 120 }

This page was automatically generated by Maven